<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/wye.jpg" alt="" />
      </div>
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
        ref="loginFormRef"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user-filling"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-14"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="warning" @click="validation">登录</el-button>
          <el-button type="danger" @click="loginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          {
            required: true,
            message: "请输入活动名称",
            trigger: "blur",
          },
          {
            min: 3,
            max: 5,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入活动名称",
            trigger: "blur",
          },
          {
            min: 5,
            max: 10,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loginReset() {
      this.$refs.loginFormRef.resetFields();
    },
    validation() {
      this.$refs.loginFormRef.validate(async (valid) => {
        //valid为boolean类型
        // console.log(valid); true or false
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm); //res 令名为 data
        if (res.meta.status !== 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");

        // 登录成功后在sessionStorage中设置令牌，令牌就是登录数据的（data.token）在数据库中可以看到
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: rgba(161, 94, 73, 0.986);
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px; //加上solid阴影设置多少他都在这
  border-radius: 50%;
  padding: 10px;
  background-color: rgb(190, 56, 22);
  box-shadow: 0px 0px 5px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.login_form {
  position: absolute;
  width: 100%;
  padding: 0 20px;
  bottom: 0;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
